package cn.yeziji.file.controller;

import cn.yeziji.file.annotation.Auth;
import cn.yeziji.file.dto.FileDTO;
import cn.yeziji.file.service.FileService;
import cn.yeziji.forum.basic.file.DirectUploaderFilesInfo;
import cn.yeziji.forum.basic.file.WebUploader;
import cn.yeziji.forum.common.ForumResult;
import cn.yeziji.forum.dto.file.ForumFilePagingDTO;
import cn.yeziji.forum.utils.ForumResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 文件控制器
 *
 * @author gzkemays
 * @since 2022/5/12 9:19
 */
@RestController
@RequestMapping("/file")
@CrossOrigin(origins = "*")
public class FileController {
  @Resource FileService fileService;

  @Auth
  @PostMapping("/get")
  public ForumResult getFile(FileDTO dto) {
    return ForumResultUtils.ok(fileService.getFile(dto));
  }

  @PostMapping("/process")
  public ForumResult getProcess(String md5) {
    return ForumResultUtils.ok(fileService.getProcess(md5));
  }

  @Auth
  @PostMapping("/upload")
  public ForumResult upload(WebUploader uploader) {
    return ForumResultUtils.ok(fileService.locationUploader(uploader));
  }

  @PostMapping("/cloudUpload")
  public ForumResult cloudUpload(WebUploader uploader) {
    return ForumResultUtils.ok(fileService.ossUploader(uploader));
  }

  @PostMapping("/directCloud")
  public ForumResult directCloud(DirectUploaderFilesInfo filesInfo) {
    return ForumResultUtils.ok(fileService.directCloud(filesInfo));
  }

  @Auth
  @PostMapping("/del")
  public ForumResult del(FileDTO dto) {
    return ForumResultUtils.ok(fileService.deleteFile(dto));
  }

  @Auth(header = "admin")
  @GetMapping("/code")
  public ForumResult code(Integer num) {
    return ForumResultUtils.ok(fileService.getCodes(num));
  }

  @Auth(header = "admin")
  @GetMapping("/searchCodes")
  public ForumResult searchCodes() {
    return ForumResultUtils.ok(fileService.searchCodes());
  }

  @Auth
  @GetMapping("/list")
  public ForumResult getUserFiles() {
    return ForumResultUtils.ok(fileService.getUserFiles());
  }

  @GetMapping("/docs")
  public ForumResult getDocs(ForumFilePagingDTO dto) {
    dto.setScope("docs");
    return ForumResultUtils.ok(fileService.getDataFiles(dto));
  }

  @PostMapping("/list")
  public ForumResult getFiles(FileDTO dto) {
    return ForumResultUtils.ok(fileService.getFiles(dto));
  }
}
